/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.services.saving;

/**
 *
 * @author jay
 */

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.faces.model.SelectItem;
import src.domain.saving.Savprod;
import src.services.CRUEDService;
import src.util.Utils;

public class SavprodModel implements java.io.Serializable {
    
    private CRUEDService service;
    
    public SavprodModel() {
        service = new CRUEDService();
    }
                  
    public List<Savprod> findAll(Map<String, String> filters) {
        return (List<Savprod>) service.findAll("Savprod", filters);
    }
    
    public List<SelectItem> listAll() {
        return service.listAll("Savprod", "savproductId","productName");
    }
    
    public Savprod getById(Integer id) {
        return (Savprod)  service.getById(Savprod.class, id);
    }
    
    public boolean isProductExist(String value, Integer id) {
        BigDecimal count;
        
        String cond = " where product_no = "+Utils.fmtSQLString(value);
        if (id != null) cond+=" and savproduct_id <> "+id;
        
        try { count = (BigDecimal) service.getObjectSQLdirect("select count(*) from savprod "+cond);
        } catch (RuntimeException e) {count = BigDecimal.ZERO;}
        
        return (count.compareTo(BigDecimal.ZERO)==0 ? false : true);
    }
    
    public List<Integer> splitSavtransId(String id) {
        List<Integer> ls = new ArrayList<>();
                
        String delimiter = ",";
        String[] temp;
        temp = id.split(delimiter);
  
        for(int i =0; i < temp.length ; i++){
            ls.add(Integer.parseInt(temp[i]));
        }
        return ls;
    }
    
    public String joinSavtransId(List<Integer> ids) {
        String temp = "";
        
        for(int i =0; i < ids.size(); i++){
           Object intgr = ids.get(i);
           String str = (String) intgr;
           temp = temp + str + ",";
        }
              
        if (temp.length() > 0)
            temp = temp.substring(0, temp.length()-1);
        return temp;
    }
        
    public boolean validateProdcut(Integer custId, Integer prodId) {
        BigDecimal obj = (BigDecimal) service.getObjectSQLdirect("select saving_modul.validate_savprod("+custId+","+prodId+") from dual");
        return (obj.intValue()==1 ? true:false);
    }
    
    public boolean saveData(Savprod data) {
        return service.saveData(data);
    }
    
    public boolean deleteData(Savprod data) {
        return service.deleteData(data);
    }
}
